const express = require('express')
const router = express.Router()
const multer = require('multer')
const path = require('path')
const expressJoi = require('@escook/express-joi')

const rank_handler = require('../router_handler/shop')
const { add_shop_schema, add_goods_schema, change_goods_status_schema } = require('../schema/shop')

let myFileName = ''
const upload = multer({
  dest: path.join(__dirname, '../uploads'),
  filename: (req,file,cb) => {
    myFileName = `${file.originalname}.${file.originalname.split('.').splice(-1)}`
    cb(null, myFileName)
  }
})

// 获取商品列表路由
router.get('/shoplist', rank_handler.shopList)

// 添加购物车路由
router.post('/addshop', expressJoi(add_shop_schema), rank_handler.addShop)

// 获取用户购物车列表
router.get('/usershoplist',rank_handler.userShopList)

// 用户获取购买记录列表
router.get('/usershopgoodslist', rank_handler.userShopGoodsList)

// 获取商品信息
router.get('/usershopinfo', rank_handler.userShopInfo)

// 移除购物车商品
router.post('/delshopinfo',expressJoi(add_shop_schema), rank_handler.delShopInfo)

// 购买商品
router.post('/shopgoods', expressJoi(add_shop_schema), rank_handler.shopGoods)

// 发布商品
router.post('/addgoodsinfo', upload.single('shop_img'), expressJoi(add_goods_schema), rank_handler.addGoodsInfo)

// 获取所有用户购买记录列表
router.get('/usershoppinglist', rank_handler.userShoppingList)

// 更改物流状态
router.post('/changestatus', expressJoi(change_goods_status_schema), rank_handler.changeGoodsStatus)

// 下架商品
router.post('/delgoodsinfo', expressJoi(add_shop_schema), rank_handler.delGoodsInfo)
module.exports = router